<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiUserController extends BaseController4Restful
{
    /**
     * @Route("/api/users", name="apiGetUsers",methods={"GET","POST"})
     */
    public function apiGetUsers(UserRepository $repository,Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $User User*/
                $User = $this->createEntity($data,User::class);
                $this->myEm()->persist($User);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $User);
            default:
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page,$total,$limit);
                $list = $repository->findBy(array(),array(),$limit,$offset);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/users/{id}", name="apiAddUsers",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateUsers(User $User,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($User);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $User User*/
                $User = $this->updateEntity($data,User::class,$User);
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($User);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $User);
    }


    /**
     * @Route("/api/save/user", name="apiSaveUser")
     */
    public function apiSaveUser()
    {
        $user = $this->needToBeUser();
        $data = $this->getRequestData();
        /**@var $User User*/
        $user = $this->updateEntity($data,User::class,$user);
        $this->myEm()->persist($user);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $user);
    }


    /**
     * @Route("/api/user/info", name="apiUserInfos")
     */
    public function apiUserInfos(Request $request)
    {
        $user = $this->needToBeUser();
        $user = $this->normalize($user);
        $url = "https://".$request->server->get("HTTP_HOST").$this->generateUrl("we_chat_scan",array("p"=>$user["id"]));
        $user["qr"] = "http://".$request->server->get("HTTP_HOST").$this->generateUrl("Qr",array("code"=>$url));
        return $this->json_out(200,"获取成功", $user);
    }


    /**
     * @Route("/api/user/get_sons", name="apiUserGetSons")
     */
    public function apiUserGetSons(Request $request)
    {
        $user = $this->needToBeUser();

        $sons = $user->getSons();
        return $this->json_out(200,"获取成功", $sons);
    }
}
